/**********************************************************************/
/*
Author: Karan Makkar
Created: Nov 2023
Description: Batch Timelines, Applications and Wins
Output: TableA2
*/
/**********************************************************************/


/*----------------------------------------------------*/
* Section: Setup
/*----------------------------------------------------*/

* include filepaths 
if "$master_run" !="1" include "./Do/SET_FILEPATHS.do"

clear all
set more off
set matsize 11000

* Log
cap log close
global prefix: display %tdCYND td(`c(current_date)')
log using "$KP_logs/${prefix}_timeline_applywin.txt", text replace

/*----------------------------------------------------*/
* Section: Import/Merge
/*----------------------------------------------------*/

import excel "$KP_deid_admin/Raw/00 Kartu Prakerja Batch Schedule.xlsx", sheet("Batch opening schedule") firstrow clear

replace batch = subinstr(batch, "*", "", .)
destring batch, replace

keep if batch <=22

destring Beneficiariesperbatch, replace
rename Beneficiariesperbatch quota

keep quota batch announcement registrationclose

tempfile quota
sa `quota', replace

u "$KP_deid_admin/Clean/pmo_b1-22_clean_long_deid.dta", clear

fmerge m:1 batch using `quota', assert(3) keep(3) nogen

gisid anon_id4 batch

bysort batch: gen applicant_count = _N

bysort batch: egen winner_count = total(win_in_batch)

bysort batch: keep if _n ==1

/*----------------------------------------------------*/
* Section: Make Table
/*----------------------------------------------------*/
label var applicant_count "Applicant Count (Admin Data)"
label var batch "Batch"
label var winner_count "Winner Count (Admin Data)"
label var registrationclose "Enrollment Deadline"
label var quota "Batch Winner-Quota"

estpost tabstat registrationclose quota applicant_count winner_count, by(batch)
esttab using $KP_output/tables/descriptive/tex/TableA2.tex, cells("registrationclose(fmt(%tdMon_DD_CCYY) label(\multicolumn{1}{c}{Enrollment})) quota(fmt(%9.0fc) label(\multicolumn{1}{c}{Batch Winner})) applicant_count(fmt(%9.0fc) label(\multicolumn{1}{c}{Applicant Count})) winner_count(fmt(%9.0fc) label(\multicolumn{1}{c}{Winner Count}))") noobs nonum nomtitle ///
varlabels(`e(labels)') drop(Total) varwidth(30) ///
collab(, lhs("`:var lab batch'")) tex replace ///
prehead(`"\hline \hline"') ///
posthead(`"\multicolumn{1}{c}{} &\multicolumn{1}{c}{Deadline}&\multicolumn{1}{c}{Quota}&\multicolumn{1}{c}{(Admin Data)}&\multicolumn{1}{c}{(Admin Data)} \\"'  `"(1) & (2) & (3) & (4) & (5) \\"' `"\hline"') ///
postfoot(`"\hline"') fragment